public class Test31 {
    public static int findGreatesSumArray(int[] arr){
        if (arr == null || arr.length < 1) return -1;
        int max = Integer.MIN_VALUE;
        int curmax = 0;
        for (int i : arr){
            if (curmax <= 0) curmax = i;
            else curmax += i;
            if (max < curmax) max = curmax;
        }
        return max;
    }

    public static int findGreatasSumArray2(int[] arr){
        int[] dp = new int[arr.length];
        dp[0] = arr[0];
        int max = Integer.MIN_VALUE;
        for (int i =1;i<arr.length;i++){
            if (dp[i-1] <= 0) dp[i] = arr[i];
            else dp[i] = dp[i-1]+arr[i];
            if (max < dp[i]) max = dp[i];
        }
        return max;
    }


    public static void main(String[] args) {
        int[] data = {1, -2, 3, 10, -4, 7, 2, -5};
        System.out.println(findGreatasSumArray2(data));

    }
}
